﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //TODO: check if a session is already established, redirect if YES
        if ((Session["username"] != null) && (Session["username"].ToString().CompareTo("") != 0))
        {
            Response.Redirect("Default.aspx");
        }
    }

    protected void Login_Button_Click(object sender, EventArgs e)
    {
        Result.Visible = false;

        String username = Username.Text;
        String password = Password.Text;

        var Client =new  BusinessTier.DataManipulation();
        bool result =(bool)Client.LoginCheck (username, password);
        

        if (result)
        {
            Session["username"] = username;
            String redirect = "Default";

            if (Request.QueryString["Redirect"] != null)
            {
                // redirect user to another page than success
                redirect = Request.QueryString["Redirect"];
            }

            Response.Redirect(redirect + ".aspx");
            // DEBUG: Result.Text = "Login sucess";
        }
        else
        {
            Result.Text = "Login failed";
        }
        Result.Visible = true; // display error
    }
}